package vn.kiemviec.app.fragments;
/**
 * TrungTT
 * 20-Mar-2013
 */

import vn.kiemviec.app.utils.ViewHelper;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class BaseFragment extends Fragment {

	public abstract void onSetUpView();
	public abstract void onSetUpEventHandler();
	
	public BaseFragment FragParrent;
	public NavFragment NavParrent;
	
	private View fragView;
	
	private int containerID;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}
	
	public int OnGetLayoutResID(){
		return 0;
	}
	
	public NavFragment getNaviFragment(){
		return NavParrent;
	}
	
	public int getFragContaner(){
		return containerID;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		int layoutRes = OnGetLayoutResID();
		if(layoutRes != 0){
			fragView = inflater.inflate(layoutRes, null);
			onSetUpView();
			onSetUpEventHandler();
			return fragView;
		}
		return super.onCreateView(inflater, container, savedInstanceState);
	}
	
	public  <V extends View> V findView(int res){
		return ViewHelper.findView(fragView, res);
	}
	
	public void addIntoContainer(int resContainer,FragmentManager fm){
		
		containerID = resContainer;		
		FragmentTransaction ft 	= fm.beginTransaction();
		
		ft.add(resContainer, this);
		ft.commit();		
	}
	
	public void pushFragment(BaseFragment fr){
	
		FragmentManager fm 		= getFragmentManager();
		FragmentTransaction ft 	= fm.beginTransaction();
		//ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
		ft.add(containerID, fr);
		ft.commit();
		
//		hideFragment(this);

//		fr.NavParrent = this.NavParrent;
//		fr.FragParrent = this;
	}
	
	public void pushFragment(BaseFragment fr,Bundle param){
		
		FragmentManager fm 		= getFragmentManager();
		FragmentTransaction ft 	= fm.beginTransaction();
		fr.setArguments(param);
		//ft.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out);
		ft.add(containerID, fr);
		ft.commit();
		
//		hideFragment(this);

//		fr.NavParrent = this.NavParrent;
//		fr.FragParrent = this;
	}
//	
//	public void pushFragment(NavFragment nav, BaseFragment fr){
//		NavParrent = nav;
//		
//		FragmentManager fm 		= getFragmentManager();
//		FragmentTransaction ft 	= fm.beginTransaction();
//		
//		ft.add(containerID, fr);
//		ft.commit();
//		hideFragment(this);
//		
//		fr.FragParrent = this;
//	}
//	
//	public void popFragment(){
//		FragmentManager fm 		= getFragmentManager();
//		FragmentTransaction ft 	= fm.beginTransaction();
//		ft.remove(this);
//		ft.show(this.FragParrent);
//		ft.commit();
//		
//	}
	
	public void hideFragment(BaseFragment fr){
		FragmentManager fm 		= getFragmentManager();
		FragmentTransaction ft 	= fm.beginTransaction();
		ft.hide(fr);
		ft.commit();
	}
	
	public void showFragment(BaseFragment fr){

		FragmentManager fm 		= getFragmentManager();
		FragmentTransaction ft 	= fm.beginTransaction();
		ft.show(fr);
		ft.commit();

	}
	
	public void showActivity(Class<?> activityClass){
		Intent intent = new Intent(getActivity(),activityClass);
		startActivity(intent);
	}
	
}
